package recycle.controller;

import io.vertx.core.MultiMap;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import recycle.common.ResponseHandler;
import recycle.pojo.Appointment;
import recycle.service.AppointmentService;

public class AppointmentController {
    private final AppointmentService appointmentService;

    public AppointmentController(AppointmentService appointmentService) {
        this.appointmentService = appointmentService;
    }

    public void setupRoutes(Router router) {

        router.post("/appointments/create").handler(this::create);
        router.delete("/appointments/delete/:id").handler(this::delete);
        router.put("/appointments/update").handler(this::update);
        router.get("/appointments/details/:id").handler(this::details);
        router.post("/appointments/list").handler(this::list);
        router.get("/appointments/listMy").handler(this::listMy);
        router.put("/appointments/feedback/:id/:feedback").handler(this::feedback);
        router.put("/appointments/finish/:id").handler(this::finish);
    }



    private void create(RoutingContext ctx) {
        Appointment appointment = ctx.body().asPojo(Appointment.class);
        appointmentService.create(appointment).onComplete(ResponseHandler.handle(ctx));
    }

    private void list(RoutingContext ctx) {
        Appointment appointment = ctx.body().asPojo(Appointment.class);
        MultiMap params = ctx.request().params();
        Integer pageNum = params.get("pageNum") != null ? Integer.parseInt(params.get("pageNum")) : 1;
        Integer pageSize = params.get("pageSize")!= null ? Integer.parseInt(params.get("pageSize")) : 10;
        appointmentService.list(appointment,pageNum,pageSize).onComplete(ResponseHandler.handle(ctx));
    }

    private void listMy(RoutingContext ctx) {
        appointmentService.listMy().onComplete(ResponseHandler.handle(ctx));
    }

    private void details(RoutingContext ctx) {
        Long id = Long.parseLong(ctx.pathParam("id"));
        appointmentService.details(id).onComplete(ResponseHandler.handle(ctx));
    }


    private void update(RoutingContext ctx) {
        Appointment appointment = ctx.body().asPojo(Appointment.class);
        appointmentService.update(appointment).onComplete(ResponseHandler.handle(ctx));
    }

    private void feedback(RoutingContext ctx) {
        Long id = Long.parseLong(ctx.pathParam("id"));
        String feedback = ctx.pathParam("feedback");
        Appointment appointment = new Appointment();
        appointment.setId(id);
        appointment.setFeedback(feedback);
        appointmentService.update(appointment).onComplete(ResponseHandler.handle(ctx));
    }

    private void finish(RoutingContext ctx) {
        Long id = Long.parseLong(ctx.pathParam("id"));
        appointmentService.finishById(id).onComplete(ResponseHandler.handle(ctx));
    }

    private void delete(RoutingContext ctx) {
        Long id = Long.parseLong(ctx.pathParam("id"));
        appointmentService.delete(id).onComplete(ResponseHandler.handle(ctx));
    }

}